package cors

import (
	"errors"
	"log/slog"
	"net/http"

	"github.com/go-chi/chi/v5/middleware"
	"github.com/go-chi/render"
	"gitverse.ru/z3nyk3y/investcalc/internal/http-server/response"
)

var ErrInvalidTask = errors.New("invalid task")
var ErrTargetValue = errors.New("target must be from 1 to 1 000 000 000 000 000")
var ErrStartUpCapValue = errors.New("startup capital must be from 1 to 1 000 000 000 000 000")
var ErrInvestPeriodStruct = errors.New("invest period cannot be null")
var ErrInvestPeriodAmount = errors.New("invest period must be from 1 to 1 000 000 000 000 000")
var ErrInvestPeriod = errors.New("invalid investment period")
var ErrCoefficientValue = errors.New("coefficient must be from 1 to 100")
var ErrReinvestPeriod = errors.New("invalid reinvest period")

type Handler struct {
	log *slog.Logger
}

func NewHandler(log *slog.Logger) *Handler {
	return &Handler{
		log: log,
	}
}

func (h *Handler) Cors() http.Handler {
	fn := func(w http.ResponseWriter, r *http.Request) {
		const op = "handlers.url.calcCors"

		log := h.log.With(
			slog.String("op", op),
			slog.String("request_id", middleware.GetReqID(r.Context())),
		)

		log.Info("CORS sended")
		w.Header().Add("Access-Control-Allow-Origin", "*")
		w.Header().Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
		w.Header().Add("Access-Control-Allow-Headers", "Content-Type")
		render.JSON(w, r, response.Error("failed to decode request"))

	}

	return http.HandlerFunc(fn)
}
